/**
*   \file   sphereaabb.h
*   \author Dave Reid
*   \brief  Header file for checking for a collision between a Sphere and a line.
*/
#ifndef __EAGL_INTERSECT_SPHEREAABB_H_
#define __EAGL_INTERSECT_SPHEREAABB_H_

#include "../types.h"
#include "../ClosestPoint/aabbtopoint.h"
#include "../Distance/aabbtopoint.h"

namespace eagl
{

/**
*   \brief  Structure for storing the result of an intersection test between a sphere and an AABB.
*/
struct INTERSECT_RESULT_SPHEREAABB
{
    /// The position of intersection.
    vec3 intersectPos;
};

/**
*   \brief                    Checks if a sphere and an AABB are intersecting.
*   \param  sphereRadius [in] The radius of the sphere.
*   \param  spherePos    [in] The position of the sphere.
*   \param  aabbExtents  [in] The extents of the AABB.
*   \param  aabbPos      [in] The position of the AABB.
*   \return                   True if the sphere and the AABB intersect.
*/
bool intersect_sphereaabb(float sphereRadius, const vec3 &spherePos,
                          const vec3 &aabbExtents, const vec3 &aabbPos,
                          INTERSECT_RESULT_SPHEREAABB *result = NULL)
{
    if (result == NULL)
    {
        float dist = distance_aabbtopoint_sq(aabbExtents, aabbPos, spherePos);

        return dist <= sphereRadius * sphereRadius;
    }
    else
    {
        closestpoint_aabbtopoint(aabbExtents, aabbPos, result->intersectPos);

        vec3 v = result->intersectPos - spherePos;
        return glm::dot(v, v) <= sphereRadius * sphereRadius;
    }
}

}

#endif  // __EAGL_INTERSECT_SPHEREAABB_H_